﻿using System;
using System.Globalization;
using System.IO;

namespace PPRecorder
{
    public class XMLCreator
    {
        public bool makeXML(string name, string author, string lootbox, int lootboxEntry, int bagslots, bool sgrey, bool swhite, bool sgreen, bool sblue, bool spurple, bool mgrey, bool mwhite, bool mgreen, bool mblue, bool mpurple)
        {
            try
            {
                var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format(@"Plugins/PPRecorder/Profiles/"));  // folder location
                if (!Directory.Exists(dir))  // if it doesn't exist, create
                    Directory.CreateDirectory(dir);

                if (!File.Exists(Path.Combine(dir, name + ".xml")))
                {
                    var path = dir + name + ".xml";
                    using (StreamWriter writer = new StreamWriter(path))
                    {
                        writer.WriteLine("<!--");
                        writer.WriteLine("\t Created with Pick Pocket Recorder by ikbeneenpeer");
                        writer.WriteLine("\t Profile by " + author);
                        writer.WriteLine("\t Generated on " + DateTime.Now.ToString("dd/MM/yyyy") + "");
                        writer.WriteLine("-->");
                        writer.Close();
                        GenerateProfile(path, name, lootbox, lootboxEntry, bagslots, sgrey, swhite, sgreen, sblue, spurple, mgrey, mwhite, mgreen, mblue, mpurple);
                    }
                }
                else
                {
                    var path = dir + name + "-1.xml";
                    using (StreamWriter writer = new StreamWriter(path))
                    {
                        writer.WriteLine("<!--");
                        writer.WriteLine("\t Created with Pick Pocket Recorder by ikbeneenpeer");
                        writer.WriteLine("\t Profile by {0}", author);
                        writer.WriteLine("\t Generated on " + DateTime.Now.ToString("dd/MM/yyyy") + "");
                        writer.WriteLine("-->");
                        writer.Close();
                        GenerateProfile(path, name, lootbox, lootboxEntry, bagslots, sgrey, swhite, sgreen, sblue, spurple, mgrey, mwhite, mgreen, mblue, mpurple);
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Write(e.Message);
                return false;
            }
        }

        public string Tab(int amount)
        {
            string tabs = "";
            int count = 0;
            if (amount >= 1)
            {
                while (count <= amount)
                {
                    count++;
                    tabs += "\t";
                }
                return tabs;
            }
            return tabs;
        }

        public bool GenerateProfile(string path, string profilename, string lootbox, int lootboxEntry, int bagslots, bool sgrey, bool swhite, bool sgreen, bool sblue, bool spurple, bool mgrey, bool mwhite, bool mgreen, bool mblue, bool mpurple)
        {
            try
            {
                int mobNumber = 0;
                using (StreamWriter writer = new StreamWriter(path, true))
                {
                    writer.WriteLine("<HBProfile>");
                    writer.WriteLine("<name>{0}</name>", profilename);
                    writer.WriteLine("");
                    writer.WriteLine("<MinLevel>1</MinLevel>");
                    writer.WriteLine("<MaxLevel>101</MaxLevel>");
                    writer.WriteLine("<MinDurability>-1</MinDurability>");
                    writer.WriteLine("<MinFreeBagSlots>-1</MinFreeBagSlots>");
                    writer.WriteLine("");
                    writer.WriteLine("<SellGrey>{0}</SellGrey>", sgrey.ToString(new CultureInfo("en-US")));
                    writer.WriteLine("<SellWhite>{0}</SellWhite>", swhite.ToString(new CultureInfo("en-US")));
                    writer.WriteLine("<SellGreen>{0}</SellGreen>", sgreen.ToString(new CultureInfo("en-US")));
                    writer.WriteLine("<SellBlue>{0}</SellBlue>", sblue.ToString(new CultureInfo("en-US")));
                    writer.WriteLine("<SellPurple>{0}</SellPurple>", spurple.ToString(new CultureInfo("en-US")));
                    writer.WriteLine("");
                    writer.WriteLine("<MailGrey>{0}</MailGrey>", mgrey.ToString(new CultureInfo("en-US")));
                    writer.WriteLine("<MailWhite>{0}</MailWhite>", mwhite.ToString(new CultureInfo("en-US")));
                    writer.WriteLine("<MailGreen>{0}</MailGreen>", mgreen.ToString(new CultureInfo("en-US")));
                    writer.WriteLine("<MailBlue>{0}</MailBlue>", mblue.ToString(new CultureInfo("en-US")));
                    writer.WriteLine("<MailPurple>{0}</MailPurple>", mpurple.ToString(new CultureInfo("en-US")));
                    writer.WriteLine("");
                    writer.WriteLine("<Vendors>");
                    writer.WriteLine(Tab(1) + Utilities.DhInstance.vendors[0]);
                    writer.WriteLine("</Vendors>");
                    writer.WriteLine("");
                    writer.WriteLine("<Mailboxes>");
                    writer.WriteLine(Tab(1) + Utilities.DhInstance.mailboxes[0]);
                    writer.WriteLine("</Mailboxes>");
                    writer.WriteLine("");
                    writer.WriteLine(Tab(1) + "<QuestOrder>");
                    writer.WriteLine("");
                    writer.WriteLine(Tab(2) + "<While Condition=\"Me.Level > 1\">");
                    writer.WriteLine("");
                    writer.WriteLine(Tab(3) + "<If Condition=\"(Me.FreeBagSlots &lt;= {0}) || (Me.LowestDurabilityPercent &lt;= 0.1)\" >", bagslots.ToString(new CultureInfo("en-US")));
                    writer.WriteLine(Tab(4) + "<CustomBehavior File=\"Message\" Text=\"Bags are (almost) full. Repairing/vendoring &amp; mailing!\" LogColor=\"CornflowerBlue\" />");
                    writer.WriteLine(Tab(4) + "<CustomBehavior File=\"FlyTo\" X=\"{0}\" Y=\"{1}\" Z=\"{2}\" />", Utilities.DhInstance.vendorX.ToString(new CultureInfo("en-US")), Utilities.DhInstance.vendorY.ToString(new CultureInfo("en-US")), Utilities.DhInstance.vendorZ.ToString(new CultureInfo("en-US")));
                    writer.WriteLine(Tab(4) + "<CustomBehavior File=\"ForcedDismount\" />");
                    writer.WriteLine(Tab(4) + "<CustomBehavior File=\"ForceSetVendor\" DoMail=\"True\" DoRepair=\"True\" DoSell=\"True\" DoTrain=\"False\" />");
                    writer.WriteLine(Tab(4) + "<CustomBehavior File=\"ForcedDismount\" />");
                    writer.WriteLine(Tab(3) + "</If>");
                    writer.WriteLine("");
                    writer.WriteLine(Tab(3) + "<While Condition=\"(Me.FreeBagSlots &gt; {0}) &amp;&amp; (Me.LowestDurabilityPercent &gt; 0.1)\">", bagslots.ToString(new CultureInfo("en-US")));
                    writer.WriteLine(Tab(4) + "<If Condition=\"Me.IsAlive\">");
                    writer.WriteLine(Tab(5) + "<CustomBehavior File=\"ForcedMount\" MountType=\"Flying\" />");
                    writer.WriteLine(Tab(5) + "<CustomBehavior File=\"Message\" Text=\"Flying to the start of the run!\" LogColor=\"CornflowerBlue\" />");
                    writer.WriteLine(Tab(5) + "<CustomBehavior File=\"FlyTo\" X=\"{0}\" Y=\"{1}\" Z=\"{2}\" />", Utilities.DhInstance.startX.ToString(new CultureInfo("en-US")), Utilities.DhInstance.startY.ToString(new CultureInfo("en-US")), Utilities.DhInstance.startZ.ToString(new CultureInfo("en-US")));
                    writer.WriteLine(Tab(5) + "<CustomBehavior File=\"ForcedDismount\" />");
                    writer.WriteLine(Tab(5) + "<CustomBehavior File=\"UserSettings\" UseMount=\"False\" LootMobs=\"False\" SkinMobs=\"False\" NinjaSkin=\"False\" HarvestHerbs=\"False\" HarvestMinerals=\"False\" PullDistance=\"1\" KillBetweenHotspots=\"False\" RessAtSpiritHealers=\"False\" LootRadius=\"30\" LogoutForInactivity=\"True\" />");
                    writer.WriteLine("");
                    writer.WriteLine(Tab(5) + "<While Condition=\"HasItem({0})\">", lootboxEntry);
                    writer.WriteLine(Tab(6) + "<CustomBehavior File=\"Message\" Text=\"Opening lockboxes before starting to keep our bags clean.\" LogColor=\"CornflowerBlue\" />");
                    writer.WriteLine(Tab(6) + "<CustomBehavior File=\"RunMacro\" Macro=\"/cast Pick lock\" NumOfTimes=\"1\"/>");
                    writer.WriteLine(Tab(6) + "<CustomBehavior File=\"RunMacro\" Macro=\"/use {0}\" NumOfTimes=\"1\" />", lootbox);
                    writer.WriteLine(Tab(6) + "<CustomBehavior File=\"RunMacro\" Macro=\"/use {0}\" NumOfTimes=\"1\" WaitTime=\"0\" />", lootbox);
                    writer.WriteLine(Tab(5) + "</While>");
                    writer.WriteLine("");
                    foreach (var mob in Utilities.DhInstance.spots)
                    {
                        mobNumber++;
                        writer.WriteLine(Tab(5) + "<!-- PPRecorder - Mob Number: {0} - Please do not touch. Upcoming feature will be using this line! -->", mobNumber.ToString(new CultureInfo("en-US")));
                        writer.WriteLine("");
                        writer.WriteLine(Tab(5) + "<If Condition=\"!Me.HasAura(&quot;Stealth&quot;)\">");
                        writer.WriteLine(Tab(6) + "<CustomBehavior File=\"RunMacro\" Macro=\"/cast Stealth\" />");
                        writer.WriteLine(Tab(5) + "</If>");
                        writer.WriteLine("");
                        writer.WriteLine(Tab(5) + "<If Condition=\"Me.HasAura(&quot;Stealth&quot;)\">");
                        writer.WriteLine(Tab(6) + mob.Replace("@", Environment.NewLine + Tab(6)));
                        writer.WriteLine(Tab(6) + "<CustomBehavior File=\"RunMacro\" Macro=\"/cast Sap\" NumOfTimes=\"1\" WaitTime=\"100\"/>");
                        writer.WriteLine(Tab(6) + "<CustomBehavior File=\"RunMacro\" Macro=\"/cast Pick Pocket\" NumOfTimes=\"1\" />");
                        writer.WriteLine(Tab(6) + "<CustomBehavior File=\"RunMacro\" Macro=\"/cleartarget\" NumOfTimes=\"1\" WaitTime=\"0\"/>");
                        writer.WriteLine(Tab(5) + "</If>");
                        writer.WriteLine("");
                        writer.WriteLine(Tab(5) + "<While Condition=\"Me.Combat\">");
                        writer.WriteLine(Tab(6) + "<CustomBehavior File=\"RunMacro\" Macro=\"/Cast Cloak of Shadows\" WaitTime=\"700\" />");
                        writer.WriteLine(Tab(6) + "<CustomBehavior File=\"RunMacro\" Macro=\"/Cast Vanish\" WaitTime=\"700\" />");
                        writer.WriteLine(Tab(6) + "<CustomBehavior File=\"RunMacro\" Macro=\"/Cast Shadow Walk\" />");
                        writer.WriteLine(Tab(5) + "</While>");
                        writer.WriteLine("");
                    }
                    writer.WriteLine(Tab(5) + "<CustomBehavior File=\"Message\" Text=\"The run ended! We're flying back.\" LogColor=\"CornflowerBlue\" />");
                    writer.WriteLine(Tab(5) + "<CustomBehavior File=\"ForcedMount\" MountType=\"Flying\" />");
                    writer.WriteLine(Tab(5) + "<CustomBehavior File=\"FlyTo\" X=\"{0}\" Y=\"{1}\" Z=\"{2}\" />", Utilities.DhInstance.endX.ToString(new CultureInfo("en-US")), Utilities.DhInstance.endY.ToString(new CultureInfo("en-US")), Utilities.DhInstance.endZ.ToString(new CultureInfo("en-US")));
                    writer.WriteLine(Tab(4) + "</If>");
                    writer.WriteLine(Tab(3) + "</While>");
                    writer.WriteLine(Tab(2) + "</While>");
                    writer.WriteLine(Tab(1) + "</QuestOrder>");
                    writer.Write("</HBProfile>");
                    writer.Close();
                    return true;
                }
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Write(e.Message);
                return false;
            }

        }
    }
}
